package com.ruoyi.business.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.ruoyi.business.service.IBusinessOrgService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusinessOrg;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机构信息Controller
 * 
 * @author ruoyi
 * @date 2022-11-23
 */
@Controller
@RequestMapping("/business/org")
public class BusinessOrgController extends BaseController
{
    private String prefix = "business/org";

    @Autowired
    private IBusinessOrgService businessOrgService;

    @RequiresPermissions("business:org:view")
    @GetMapping()
    public String org()
    {
        return prefix + "/org";
    }

    /**
     * 查询机构信息列表
     */
//    @RequiresPermissions("business:org:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusinessOrg businessOrg)
    {
        startPage();
        List<BusinessOrg> list = businessOrgService.selectBusinessOrgList(businessOrg);
        return getDataTable(list);
    }

    /**
     * 导出机构信息列表
     */
//    @RequiresPermissions("business:org:export")
    @Log(title = "机构信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusinessOrg businessOrg)
    {
        List<BusinessOrg> list = businessOrgService.selectBusinessOrgList(businessOrg);
        ExcelUtil<BusinessOrg> util = new ExcelUtil<BusinessOrg>(BusinessOrg.class);
        return util.exportExcel(list, "机构信息数据");
    }

    /**
     * 新增机构信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存机构信息
     */
//    @RequiresPermissions("business:org:add")
    @Log(title = "机构信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("orgImgFile") MultipartFile orgImgFile, @RequestParam("orgLogoFile") MultipartFile orgLogoFile, BusinessOrg businessOrg) throws IOException {
        if(orgImgFile == null){
            return AjaxResult.error("图片文件不允许为空");
        }
        if(orgLogoFile == null){
            return AjaxResult.error("logo文件不允许为空");
        }
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileOrgImgName = FileUploadUtils.upload(filePath, orgImgFile);
        businessOrg.setOrgImg(fileOrgImgName);
        String fileOrgLogoName = FileUploadUtils.upload(filePath, orgLogoFile);
        businessOrg.setOrgLogo(fileOrgLogoName);

        return toAjax(businessOrgService.insertBusinessOrg(businessOrg));
    }

    /**
     * 修改机构信息
     */
//    @RequiresPermissions("business:org:edit")
    @GetMapping("/edit/{orgId}")
    public String edit(@PathVariable("orgId") Long orgId, ModelMap mmap)
    {
        BusinessOrg businessOrg = businessOrgService.selectBusinessOrgByOrgId(orgId);
        mmap.put("businessOrg", businessOrg);
        return prefix + "/edit";
    }

    /**
     * 修改保存机构信息
     */
//    @RequiresPermissions("business:org:edit")
    @Log(title = "机构信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "orgImgFile", required = false) MultipartFile orgImgFile,
                               @RequestParam(value = "orgLogoFile", required = false) MultipartFile orgLogoFile,
                               BusinessOrg businessOrg){
        try{
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            if(orgImgFile != null){
                // 上传并返回新文件名称
                String fileOrgImgName = FileUploadUtils.upload(filePath, orgImgFile);
                businessOrg.setOrgImg(fileOrgImgName);
            }
            if(orgLogoFile != null){
                String fileOrgLogoName = FileUploadUtils.upload(filePath, orgLogoFile);
                businessOrg.setOrgLogo(fileOrgLogoName);
            }
            return toAjax(businessOrgService.updateBusinessOrg(businessOrg));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除机构信息
     */
//    @RequiresPermissions("business:org:remove")
    @Log(title = "机构信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(businessOrgService.deleteBusinessOrgByOrgIds(ids));
    }
}
